﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


// Condition: Write a program that reads two integer numbers N and K
//            and an array of N elements from the console. Find in the 
//            array those K elements that have maximal sum.

class ElementsWithMaxSum
{
    static void Main()
    {
        // 1. Declare N, K, array of N elements and variable count
        Console.Write("Enter N: ");
        int N = int.Parse(Console.ReadLine());
        Console.Write("Enter K (smaller than N): ");
        int K = int.Parse(Console.ReadLine());
        int[] myArr = new int[N];
        Console.WriteLine("Enter elements of integer array: ");
        for (int index = 0; index < N; index++)
        {
            Console.Write("element[{0}]= ", index);
            myArr[index] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine();
        int count;


        // 2. Sort elements of array from bigger to smaller
        do
        {
          count=0;
          for (int i = 0; i < myArr.Length-1; i++)
			{
              if ( myArr[i]<myArr[i+1])
              {
                  count++;
                  myArr[i]=myArr[i]+myArr[i+1];
                  myArr[i+1]=myArr[i]-myArr[i+1];
                  myArr[i]=myArr[i]-myArr[i+1];
              }
			  
			}
        } while (count!=0);

        // 3. Take first K element of sorted array and print them on the console.

        Console.Write("K elements with maximul sum are:{ ");
        for (int i = 0; i < K; i++)
        {
            Console.Write(myArr[i]+" ");    
        }
        Console.Write("}");
        Console.WriteLine();
    }
}

